#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve()
{
    int n,m;
    cin >> n >> m;
    vector<pair<int,int>> vec(n + 10);
    for(int i = 1; i <= n; i ++)
    {
        cin >> vec[i].first >> vec[i].second;
    }
    int last = 0;
    int pos = 0;
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        int c = vec[i].first - last;
        if(vec[i].second == pos)
        {
            if(c % 2 == 0) ans += c;
            else ans += c - 1;
        }
        else
        {
            if(c % 2 == 1) ans += c;
            else ans += c - 1;
        }
        last = vec[i].first;
        pos = vec[i].second;
    }
    int c = m - last;
    if(m - last > 0)
    {
        ans += c;
    }
    cout << ans << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
}